const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const jumpWeapp = utils.appid;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({
    data: {
        tabs: '',
        uid: '',
        official: false
    },
    onLoad(options) {
        var that = this;
        var reg = RegExp(/testweapp.whwangdoudou.cn/);
        if (reg.exec(utils.url)) {
            that.setData({
                official: true
            })
        };
        that.setData({
            optionL: options,
            jumpWeapp: jumpWeapp
        });
        if (options.uid) {
            that.data.uid = options.uid;
            wx.setStorageSync('weapp_uid', that.data.uid);
        }
        var p = {
            m: 'new_index',
            ua: app.globalData.ua
        }
        post(p).then(res => {
            that.setData({
                tabs: res.result,
                uid: that.data.uid
            });
        })
    },

    tapId(e) {
        let weappid = e.currentTarget.dataset.weappid;
        let that = this;
        let timestamp = parseInt(new Date().getTime() / 1000);
        let content = {
            userinfo: wx.getStorageSync('_userinfo'),
            device_info: wx.getStorageSync('device_info')
        };
        content = JSON.stringify(content);

        let guest_time_start = parseInt(new Date().getTime() / 1000);
        wx.setStorageSync('guest_time_start_' + that.data.uid + '_' + weappid, guest_time_start);
        wx.setStorageSync('weappid', weappid);

        let p = {
            m: 'invoke',
            type: 'guest',
            content: content,
            uid: that.data.uid,
            weappid: weappid
        };
        post(p).then(res => {
            that.smsw(e)
        });
    },
    smsw(e) {
        if (!!e.target.dataset.pagema) {
            wx.previewImage({
                urls: [e.target.dataset.pagema],
            });
        }
    },
    onReady: function() {
        wx.getStorageInfo({
            success: function(res) {
                //console.log(res.keys)
                for (var i = 0; i < res.keys.length; i++) {
                    var key = res.keys[i];
                    //console.log(key);
                    if (0 == key.indexOf('guest_time_start_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('guest_time_end_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('weappid')) {
                        wx.removeStorageSync(key);
                    }
                }
            }
        });
    },

    onShow() {
        let that = this;
        app.getLbs();
        if (that.data.optionL.uid) {
            that.data.uid = that.data.optionL.uid;
            wx.setStorageSync('weapp_uid', that.data.uid);
        }
        let weappid = wx.getStorageSync('weappid');
        let guest_time_start = wx.getStorageSync('guest_time_start_' + that.data.uid + '_' + weappid)
        if (that.data.uid && weappid) {
            if (guest_time_start && guest_time_start > 0) {
                let guest_time_end = parseInt(new Date().getTime() / 1000);
                wx.setStorageSync('guest_time_end_' + that.data.uid + '_' + weappid, guest_time_end);
                let guest_time = guest_time_end - guest_time_start;
                let content = {
                    userinfo: wx.getStorageSync('_userinfo'),
                    device_info: wx.getStorageSync('device_info'),
                    guest_time: guest_time
                };
                content = JSON.stringify(content);
                let p = {
                    m: 'invoke',
                    type: 'guest_end',
                    content: content,
                    uid: that.data.uid,
                    weappid: weappid
                };
                post(p).then(res => {
                    wx.removeStorageSync('weappid');
                    wx.removeStorageSync('guest_time_start_' + that.data.uid + '_' + weappid);
                    wx.removeStorageSync('guest_time_end_' + that.data.uid + '_' + weappid);
                    if (guest_time < 30) {

                    } else {

                    }
                })
            }
        }
    },
    submitFormId(e) {
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let that = this;
        let p = {
            m: 'colform',
            uid: that.data.uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        //console.log(p);
        if (that.data.uid) {
            post(p).then(res => {})
        }

    }
})